//
//  EJSCouponViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/19.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSCouponViewController.h"
#import "EJSCouponCCell.h"
#import "EJSCouponRequest.h"
#import "XOShopTabBarController.h"
#import "XOShopViewModel.h"

@interface EJSCouponViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation EJSCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"我的优惠券";
    if(self.dataSource.count == 0){
        [self getCouponList];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configOwnViews
{
    [self.collectionView registerNib:[UINib nibWithNibName:@"EJSCouponCCell" bundle:nil] forCellWithReuseIdentifier:@"EJSCouponCCell"];
}

- (void)getCouponList{
    EJSCouponRequest *req = [EJSCouponRequest new];
    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        EJSCouponListModel *model = response.data;
        weakSelf.dataSource = [NSMutableArray arrayWithArray:model.list];
        if(weakSelf.dataSource.count > 0){
            [weakSelf hideNoDataView];
        }else{
            [weakSelf showNoDataViewWithText:@"暂无优惠券"];
        }
        [weakSelf.collectionView reloadData];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}


#pragma mark - UICollectionViewDataSouce
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    EJSCouponCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSCouponCCell" forIndexPath:indexPath];
    [cell setCouponModel:self.dataSource[indexPath.item]];
    __weak typeof(self)weakSelf = self;
    cell.useItBlock = ^(XOShopModel *model) {
        XOShopTabBarController *shopTabVc = [[XOShopTabBarController alloc] init];
        [XOShopViewModel shared].checkShopIndex = 0; //去商品
        [XOShopViewModel shared].shopModel = model;
        [weakSelf.navigationController pushViewController:shopTabVc animated:YES];
    };
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(ScreenWidth - 30, 100);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(self.block){
        self.block(self.dataSource[indexPath.item]);
        [self leftBarButtonAction:nil];
    }
}

@end
